//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "CompositeInstrument.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Instrument.h>
#include <gen/QL/PricingEngine.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CCompositeInstrument::CCompositeInstrument (boost::shared_ptr<QuantLib::CompositeInstrument>& childNative, Object^ owner) : CInstrument(CCompositeInstrument::typeid)
{
#ifdef HANDLE
	_phCompositeInstrument = NULL;
#endif
	_ppCompositeInstrument = &childNative;
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCompositeInstrument));
}
Cephei::QL::Instruments::CCompositeInstrument::CCompositeInstrument (QuantLib::CompositeInstrument& childNative, Object^ owner) : CInstrument(CCompositeInstrument::typeid)
{
#ifdef HANDLE
	_phCompositeInstrument = NULL;
#endif
	_ppCompositeInstrument = new boost::shared_ptr<QuantLib::CompositeInstrument> (&childNative);
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCompositeInstrument));
    _CompositeInstrumentOwner = owner;
    _InstrumentOwner = owner;
}

Cephei::QL::Instruments::CCompositeInstrument::CCompositeInstrument (CCompositeInstrument^ copy) : CInstrument(CCompositeInstrument::typeid)
{
#ifdef HANDLE
	_phCompositeInstrument = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCompositeInstrument = new boost::shared_ptr<QuantLib::CompositeInstrument> (copy->GetShared());
        _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCompositeInstrument));
    }
}
Cephei::QL::Instruments::CCompositeInstrument::CCompositeInstrument (System::Type^ t) : CInstrument(CCompositeInstrument::typeid)
{
#ifdef HANDLE
	_phCompositeInstrument = NULL;
#endif
	if (!t->IsSubclassOf(CCompositeInstrument::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CCompositeInstrument::CCompositeInstrument (QuantLib::Handle<QuantLib::CompositeInstrument>& childNative, Object^ owner)  : CInstrument(CCompositeInstrument::typeid)
{
	_phCompositeInstrument = &childNative;
	_ppCompositeInstrument = &static_cast<boost::shared_ptr<QuantLib::CompositeInstrument>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCompositeInstrument));
    _CompositeInstrumentOwner = owner;
}
Cephei::QL::Instruments::CCompositeInstrument::CCompositeInstrument (QuantLib::Handle<QuantLib::CompositeInstrument> childNative)  : CInstrument(CCompositeInstrument::typeid)
{
	_phCompositeInstrument = &childNative;
	_ppCompositeInstrument = &static_cast<boost::shared_ptr<QuantLib::CompositeInstrument>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCompositeInstrument));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CCompositeInstrument::CCompositeInstrument (QuantLib::CompositeInstrument childNative)  : CInstrument(CCompositeInstrument::typeid)
{
#ifdef HANDLE
	_phCompositeInstrument = NULL;
#endif
	_ppCompositeInstrument = new boost::shared_ptr<QuantLib::CompositeInstrument> (new QuantLib::CompositeInstrument (childNative));
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCompositeInstrument));
}
#endif

Cephei::QL::Instruments::CCompositeInstrument::~CCompositeInstrument ()
{
    if (_ppCompositeInstrument != NULL)
    {
	    delete _ppCompositeInstrument;
        _ppCompositeInstrument = NULL;
    }
}
Cephei::QL::Instruments::CCompositeInstrument::!CCompositeInstrument ()
{
    if (_ppCompositeInstrument != NULL)
    {
	    delete _ppCompositeInstrument;
    }
}
QuantLib::CompositeInstrument& Cephei::QL::Instruments::CCompositeInstrument::GetReference ()
{
    if (_ppCompositeInstrument == NULL) throw gcnew NativeNullException ();
	return **_ppCompositeInstrument;
}
boost::shared_ptr<QuantLib::CompositeInstrument>& Cephei::QL::Instruments::CCompositeInstrument::GetShared ()
{
    if (_ppCompositeInstrument == NULL) throw gcnew NativeNullException ();
	return *_ppCompositeInstrument;
}
QuantLib::CompositeInstrument* Cephei::QL::Instruments::CCompositeInstrument::GetPointer ()
{
    if (_ppCompositeInstrument == NULL) throw gcnew NativeNullException ();
	return &**_ppCompositeInstrument;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CompositeInstrument>& Cephei::QL::Instruments::CCompositeInstrument::GetHandle ()
{
	if (_phCompositeInstrument == NULL)
	{
		_phCompositeInstrument = new Handle<QuantLib::CompositeInstrument> (*_ppCompositeInstrument);
	}
	return *_phCompositeInstrument;
}
#endif
bool Cephei::QL::Instruments::CCompositeInstrument::HasNative () 
{
	return (_ppCompositeInstrument != NULL);
}

Cephei::QL::Instruments::ICompositeInstrument^ Cephei::QL::Instruments::CCompositeInstrument::Add (Cephei::QL::IInstrument^ instrument, Microsoft::FSharp::Core::FSharpOption<Double>^ multiplier)
{
    CInstrument^ _Cinstrument;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cinstrument = safe_cast<CInstrument^> (instrument);
        _Cinstrument->Lock();
        boost::shared_ptr<QuantLib::Instrument>& _instrument = static_cast<boost::shared_ptr<QuantLib::Instrument>&> (_Cinstrument->GetShared ()); 
        QuantLib::Real _multiplier = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (multiplier) ? (QuantLib::Real)ValueHelper::Convert (multiplier->Value) : 1.0); //9a
    	(*_ppCompositeInstrument)->add ( _instrument,  _multiplier );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cinstrument != nullptr) _Cinstrument->Unlock();
    }
}
Boolean Cephei::QL::Instruments::CCompositeInstrument::IsExpired::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppCompositeInstrument)->isExpired ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::ICompositeInstrument^ Cephei::QL::Instruments::CCompositeInstrument::Subtract (Cephei::QL::IInstrument^ instrument, Microsoft::FSharp::Core::FSharpOption<Double>^ multiplier)
{
    CInstrument^ _Cinstrument;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cinstrument = safe_cast<CInstrument^> (instrument);
        _Cinstrument->Lock();
        boost::shared_ptr<QuantLib::Instrument>& _instrument = static_cast<boost::shared_ptr<QuantLib::Instrument>&> (_Cinstrument->GetShared ()); 
        QuantLib::Real _multiplier = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (multiplier) ? (QuantLib::Real)ValueHelper::Convert (multiplier->Value) : 1.0); //9a
    	(*_ppCompositeInstrument)->subtract ( _instrument,  _multiplier );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cinstrument != nullptr) _Cinstrument->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

